package ex.klmy.gov.module;

import com.fasterxml.jackson.databind.util.JSONPObject;
import ex.klmy.gov.bean.News;
import ex.klmy.gov.bean.NewsDetails;
import ex.klmy.gov.bean.Owner;
import ex.klmy.gov.utils.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.ioc.annotation.InjectName;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import org.w3c.dom.NodeList;
import org.w3c.dom.traversal.NodeFilter;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by xiaguoqiang on 16/11/2.
 */
@IocBean
@InjectName
@At("/xxgk")
public class NewsAction extends BaseAction{

    @At
    @Ok("jsonp")
    public Object getNewsByTreeId(@Param("tId")String tId, @Param("pageNo")int pageNo,@Param("pageSize") int pageSize) {
        List<News> l = basicDao.searchByPage(News.class,"where owner = '831749743' and wbtreeid =" + tId,pageNo,pageSize,"wbupdatedate","desc");
        for(News n : l) {
            Owner o = basicDao.findByCondition(Owner.class, Cnd.where("wbfirmid", "=", n.getOwner()));
            if(n.getImg() != null)
            n.setImg("http://www.klmyq.gov.cn" + n.getImg().replace("${ownername}",o.getcName()));
        }
        return l;
    }

    @At
    @Ok("jsonp")
    public Object getNewsById(@Param("id") String newsId) {
        NewsDetails n = basicDao.findByCondition(NewsDetails.class, Cnd.wrap("where wbnewsid = " + newsId));
        Owner o = basicDao.findByCondition(Owner.class, Cnd.where("wbfirmid", "=", n.getOwner()));
        n.setWbcontent(StringUtils.replaceImgUrl(n.getWbcontent()));
        n.setWbcontent(n.getWbcontent().replace("${ownername}",o.getcName()));
        return n;
    }

    @At
    @Ok("jsonp")
    public Object getNewsByTreePid(@Param("tPid")String tPid, @Param("pageNo")int pageNo,@Param("pageSize") int pageSize) {
        String sql = "select wbnews.* from wbnews left join wbtree on wbnews.wbtreeid = wbtree.wbtreeid\n" +
                "where wbtree.wbtreefid = '" + tPid + "'";
            List<News> l = basicDao.searchM(News.class,sql,pageNo,pageSize);
        for(News n : l) {
            Owner o = basicDao.findByCondition(Owner.class, Cnd.where("wbfirmid", "=", n.getOwner()));
            if(n.getImg() != null)
            n.setImg("http://www.klmyq.gov.cn" + n.getImg().replace("${ownername}",o.getcName()));
        }
        return l;
    }

    @At
    @Ok("jsonp")
    public Object getTopNews() {
        List<News> l = basicDao.search(News.class,Cnd.where("wbcontentid","=","34807").and("wbtop","=","1").and("wbauditing","=","1").and("wbhastitlepic","=","1"));
        for(News n : l) {
            Owner o = basicDao.findByCondition(Owner.class, Cnd.where("wbfirmid", "=", n.getOwner()));
            if(n.getImg() != null)
                n.setImg("http://www.klmyq.gov.cn" + n.getImg().replace("${ownername}",o.getcName()));
        }
        return l;
    }

    @At
    @Ok("jsonp")
    public Object searchNews(@Param("key")String key, @Param("pageNo")int pageNo,@Param("pageSize") int pageSize) {
        List<News> l = basicDao.searchByPage(News.class,Cnd.where("wbkeyword","like","%"+ key +"%"),pageNo,pageSize);
        for(News n : l) {
            Owner o = basicDao.findByCondition(Owner.class, Cnd.where("wbfirmid", "=", n.getOwner()));
            if(n.getImg() != null && !"".equals(n.getImg()))
                n.setImg("http://www.klmyq.gov.cn" + n.getImg().replace("${ownername}",o.getcName()));
        }
        return l;
    }

    @At
    @Ok("jsonp")
    public Object searchNewsByTreeId(@Param("key")String key, @Param("tId")String tId,@Param("pageNo")int pageNo,@Param("pageSize") int pageSize) {
        List<News> l = basicDao.searchByPage(News.class,Cnd.where("wbkeyword","like","%"+ key +"%").and("wbtreeid","=",tId),pageNo,pageSize);
        for(News n : l) {
            Owner o = basicDao.findByCondition(Owner.class, Cnd.where("wbfirmid", "=", n.getOwner()));
            if(n.getImg() != null && !"".equals(n.getImg()))
                n.setImg("http://www.klmyq.gov.cn" + n.getImg().replace("${ownername}",o.getcName()));
        }
        return l;
    }

    @At
    @Ok("jsonp")
    public Object searchNewsByPtreeId(@Param("key")String key,@Param("tPid")String tPid, @Param("pageNo")int pageNo,@Param("pageSize") int pageSize) {
        String sql = "select wbnews.* from wbnews left join wbtree on wbnews.wbtreeid = wbtree.wbtreeid\n" +
                "where wbtree.wbtreefid = '" + tPid + "' and wbnews.wbkeyword like '%" + key + "%'";
        List<News> l = basicDao.searchM(News.class,sql,pageNo,pageSize);
        for(News n : l) {
            Owner o = basicDao.findByCondition(Owner.class, Cnd.where("wbfirmid", "=", n.getOwner()));
            if(n.getImg() != null)
                n.setImg("http://www.klmyq.gov.cn" + n.getImg().replace("${ownername}",o.getcName()));
        }
        return l;
    }
}
